package com.lmiot.xyclick.Bean.SQL;

import android.content.Context;
import com.lmiot.xyclick.Bean.SQL.ActionBeanDao;
import com.lmiot.xyclick.Bean.SQL.DaoMaster;
import com.lmiot.xyclick.Util.FileUtils;
import com.lmiot.xyclick.inteface.OnBasicListener;
import java.util.ArrayList;
import java.util.List;
import org.greenrobot.greendao.query.WhereCondition;

public class ActionBeanSqlUtil {
    private static final ActionBeanSqlUtil ourInstance = new ActionBeanSqlUtil();
    private ActionBeanDao mActionBeanDao;

    public static ActionBeanSqlUtil getInstance() {
        return ourInstance;
    }

    private ActionBeanSqlUtil() {
    }

    public void initDbHelp(Context context) {
        this.mActionBeanDao = new DaoMaster(new DaoMaster.DevOpenHelper(context, "ActionBean-db", null).getWritableDatabase()).newSession().getActionBeanDao();
    }

    public void add(ActionBean actionBean) {
        actionBean.setId(null);
        this.mActionBeanDao.insertOrReplace(actionBean);
        FileUtils.saveToLocalData(actionBean.getAutoID(), new OnBasicListener() {
            @Override // com.lmiot.xyclick.inteface.OnBasicListener
            public void result(boolean z, String str) {
            }
        });
    }

    public void addList(String str, List<ActionBean> list) {
        try {
            delByID(str);
            if (list != null && list.size() > 0) {
                for (ActionBean actionBean : list) {
                    actionBean.setId(null);
                }
                this.mActionBeanDao.insertOrReplaceInTx(list);
                FileUtils.saveToLocalData(str, new OnBasicListener() {
                    /* class com.lmiot.xyclick.Bean.SQL.ActionBeanSqlUtil.AnonymousClass2 */

                    @Override // com.lmiot.xyclick.inteface.OnBasicListener
                    public void result(boolean z, String str) {
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addList01(List<ActionBean> list) {
        if (list != null && list.size() > 0) {
            for (ActionBean actionBean : list) {
                actionBean.setId(null);
            }
            this.mActionBeanDao.insertOrReplaceInTx(list);
            FileUtils.saveToLocalData(list.get(0).getAutoID(), new OnBasicListener() {
                /* class com.lmiot.xyclick.Bean.SQL.ActionBeanSqlUtil.AnonymousClass3 */

                @Override // com.lmiot.xyclick.inteface.OnBasicListener
                public void result(boolean z, String str) {
                }
            });
        }
    }

    public void delByID(String str) {
        if (str == null) {
            str = "";
        }
        ArrayList arrayList = (ArrayList) this.mActionBeanDao.queryBuilder().where(ActionBeanDao.Properties.AutoID.eq(str), new WhereCondition[0]).list();
        if (arrayList.size() > 0) {
            this.mActionBeanDao.deleteInTx(arrayList);
        }
    }

    public List<ActionBean> searchByID(String str) {
        Exception e;
        ArrayList arrayList = new ArrayList();
        if (str == null) {
            str = "";
        }
        try {
            ArrayList arrayList2 = (ArrayList) this.mActionBeanDao.queryBuilder().where(ActionBeanDao.Properties.AutoID.eq(str), new WhereCondition[0]).orderAsc(ActionBeanDao.Properties.SortNum).list();
            if (arrayList2 != null) {
                return arrayList2;
            }
            try {
                return new ArrayList();
            } catch (Exception e2) {
                arrayList = arrayList2;
                e = e2;
                try {
                    e.printStackTrace();
                    return arrayList;
                } catch (Throwable unused) {
                    return arrayList;
                }
            } catch (Throwable unused2) {
                return arrayList2;
            }
        } catch (Exception e3) {
            e = e3;
            e.printStackTrace();
            return arrayList;
        }
    }

    public ActionBean searchByActionID(String str) {
        if (str == null) {
            return null;
        }
        ArrayList arrayList = (ArrayList) this.mActionBeanDao.queryBuilder().where(ActionBeanDao.Properties.ActionID.eq(str), new WhereCondition[0]).orderAsc(ActionBeanDao.Properties.SortNum).list();
        if (arrayList.size() > 0) {
            return (ActionBean) arrayList.get(0);
        }
        return null;
    }

    public List<ActionBean> searchAll() {
        List<ActionBean> list = this.mActionBeanDao.queryBuilder().orderAsc(ActionBeanDao.Properties.SortNum).build().list();
        return list == null ? new ArrayList() : list;
    }
}
